<template>
  <div class="select-year">
    <h5 class="subtitle">第三步</h5>
    <cube-form :model="model" @validate="validateHandler" @submit="submitHandler">
      <cube-form-group>
        <cube-form-item :field="fields[0]"></cube-form-item>
      </cube-form-group>
      <cube-form-group class="subBtn">
        <cube-button @click="goback" :inline="true" :primary="true">上一步</cube-button>
        <cube-button type="submit" :inline="true" :primary="true">下一步</cube-button>
      </cube-form-group>
    </cube-form>
  </div>
</template>

<script>
export default {
  name: "SelectYear",
  data() {
    return {
      validity: {},
      valid: undefined,
      model: {
        enterYear: ""
      },
      fields: [
        {
          type: "select",
          modelKey: "enterYear",
          label: "入学年份",
          props: {
            title: "入学年份",
            options: [2012, 2013, 2014, 2015, 2016, 2017, 2018],
            placeholder: "请选择入学年份"
          },
          rules: {
            required: true
          }
        }
      ]
    };
  },
  mounted() {},
  methods: {
    selectHandle() {},
    cancelHandle() {
      this.$createToast({
        type: "correct",
        txt: "取消选择",
        time: 1000
      }).show();
    },

    validateHandler(result) {
      this.validity = result.validity;
      this.valid = result.valid;
    },
    submitHandler(e) {
      e.preventDefault();
      this.$store.state.enterYear = this.model.enterYear;
      let requestData = {
        schoolId: this.$store.state.schoolId,
        enterYear: this.$store.state.enterYear
      };
      this.$http
        .post("/api000012", requestData)
        .then(response => {
          if (response.data.status == "0") {
            this.$createToast({
              type: "warn",
              time: 1000,
              txt: response.data.msg
            }).show();
          } else {
            // console.log(response.data)
            this.$store.state.classes = response.data;
            let classes=JSON.stringify(response.data)
            localStorage.setItem('classes',classes)
            this.$router.push({
              path: "/paradd/class",
              query: {
                province: this.$route.query.province,
                region: this.$route.query.region,
                schoolId: this.model.schoolId,
                enterYear: this.model.enterYear
              }
            });
          }
        })
        .catch(error => {
          this.$createToast({
            type: "error",
            txt: error,
            time: 1000
          }).show();
        });
    },
    goback() {
      this.$router.push({
        path: "/paradd/school",
        query: {
          province: this.$route.query.province,
          region: this.$route.query.region,
          schoolId: this.model.schoolId
        }
      });
    }
  }
};
</script>

<style lang="scss">
.select-year {
  .subtitle {
    font-size: 16px;
    font-weight: 700;
    color: #fc9153;
    margin-top: 20%;
    margin-bottom: 10%;
    text-align: center;
  }

  .subBtn {
    margin: 5% auto;
    text-align: center;
    margin-top: 20%;
    button {
      width: 40%;
      margin: 2%;
    }
  }
}
</style>
